package com.lk.theurlpro.controller;

import com.lk.theurlpro.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author lk
 * @date 2020/2/10 0010 13:43
 */
@Controller
public class LoginController {

    @PostMapping(value = "/doLogin" )
    public String dologin(User user) throws Exception{
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(user.getName(), user.getPassword()));
            System.out.println("ok");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("no");
            return "redirect:login";
        }
        return "redirect:index";
    }

    @GetMapping("/logout")
    public String logout(){
        return "login";
    }

    @GetMapping("/404")
    public String error(){
        return "404";
    }

    @GetMapping("/login")
    public String login(){
        return "login";
    }

}
